package com.ruoyi.basics.service.impl;

import com.ruoyi.basics.domain.WmsWarehouse;
import com.ruoyi.basics.mapper.WmsWarehouseMapper;
import com.ruoyi.basics.service.IWmsWarehouseService;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 仓库管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-09
 */
@Service
public class WmsWarehouseServiceImpl implements IWmsWarehouseService {
    @Autowired
    private WmsWarehouseMapper wmsWarehouseMapper;

    /**
     * 查询仓库管理
     *
     * @param id 仓库管理主键
     * @return 仓库管理
     */
    @Override
    public WmsWarehouse selectWmsWarehouseById(Long id) {
        return wmsWarehouseMapper.selectWmsWarehouseById(id);
    }

    /**
     * 查询仓库管理列表
     *
     * @param wmsWarehouse 仓库管理
     * @return 仓库管理
     */
    @Override
    public List<WmsWarehouse> selectWmsWarehouseList(WmsWarehouse wmsWarehouse) {
        return wmsWarehouseMapper.selectWmsWarehouseList(wmsWarehouse);
    }

    /**
     * 新增仓库管理
     *
     * @param wmsWarehouse 仓库管理
     * @return 结果
     */
    @Override
    public int insertWmsWarehouse(WmsWarehouse wmsWarehouse) {
        //判断仓库名称的唯一
        if (wmsWarehouse == null || StringUtils.isBlank(wmsWarehouse.getName())) {
            throw new IllegalArgumentException("区域名称不能为空");
        }
        // 2. 检查名称是否已存在（区分大小写）
        boolean exists = wmsWarehouseMapper.existsByAreaName(wmsWarehouse.getName());
        if (exists) {
            throw new RuntimeException("区域[" + wmsWarehouse.getName() + "]已存在");
        }

        wmsWarehouse.setCreateTime(DateUtils.getNowDate());
        //更新时间
        wmsWarehouse.setUpdateTime(DateUtils.getNowDate());
        return wmsWarehouseMapper.insertWmsWarehouse(wmsWarehouse);
    }

    /**
     * 修改仓库管理
     *
     * @param wmsWarehouse 仓库管理
     * @return 结果
     */
    @Override
    public int updateWmsWarehouse(WmsWarehouse wmsWarehouse) {
        wmsWarehouse.setUpdateTime(DateUtils.getNowDate());
        return wmsWarehouseMapper.updateWmsWarehouse(wmsWarehouse);
    }

    /**
     * 批量删除仓库管理
     *
     * @param ids 需要删除的仓库管理主键
     * @return 结果
     */
    @Override
    public int deleteWmsWarehouseByIds(Long[] ids) {
        return wmsWarehouseMapper.deleteWmsWarehouseByIds(ids);
    }

    /**
     * 删除仓库管理信息
     *
     * @param id 仓库管理主键
     * @return 结果
     */
    @Override
    public int deleteWmsWarehouseById(Long id) {
        return wmsWarehouseMapper.deleteWmsWarehouseById(id);
    }

    @Override
    public int changeStatus(Long id, Integer status) {
        // 检查ID是否有效
        if (id == null) {
            throw new IllegalArgumentException("仓库ID不能为空");
        }
        // 查询仓库信息
        WmsWarehouse warehouse = wmsWarehouseMapper.selectWmsWarehouseById(id);
        // 检查仓库是否存在
        if (warehouse == null) {
            throw new RuntimeException("未找到ID为" + id + "的仓库信息");
        }

        // 切换状态
        warehouse.setStatus(warehouse.getStatus() == 1 ? 0 : 1);


        // 更新仓库状态
        return wmsWarehouseMapper.updateWmsWarehouse(warehouse);
    }

    @Override
    public String generateWarehouseCode() {
        String maxCode = wmsWarehouseMapper.findMaxWarehouseCode();
        String prefix = "CK";
        Pattern pattern = Pattern.compile(prefix + "(\\d+)");
        int newCount = 1;
        if (maxCode != null) {
            Matcher matcher = pattern.matcher(maxCode);
            if (matcher.find()) {
                String numberPart = matcher.group(1);
                newCount = Integer.parseInt(numberPart) + 1;
            }
        }
        // 格式化数字部分为 6 位，不足补零
        return prefix + String.format("%06d", newCount);
    }


}
